#include<cstring>
#include <iostream>
using std::cout;
using std::endl;
class Computer{
public:
    static Computer * get_instance(const char*brand,int price){
        if(!instance){
            instance =  new Computer(brand,price);
        }else{
            delete []instance->_brand;
            instance->_brand = new char[strlen(brand)+1]();
            strcpy(instance->_brand,brand);
            instance->_price = price;
        }
        cout<<"Computer *get_instance"<<endl;
        return instance;
    }

    Computer(const Computer&) = delete;
    Computer &operator=(const Computer&) = delete;

    void destroy(){
        cout<<"destory()"<<endl;
        delete instance;
        instance = nullptr;
    }

    void print(){
        cout<<_brand<<":"<<_price<<endl;
    }

private:
    Computer(const char *brand,int price)
        :_brand(new char[strlen(brand)+1]())
         ,_price(price)
    {   
        cout<<"Computer(const char *,int)"<<endl;
        strcpy(_brand,brand);
    }

    ~Computer(){
        if(_brand){
            delete []_brand;
        }
        cout<<"~Computer"<<endl;
        _brand = nullptr;
    }
    static Computer *instance;
    char *_brand;
    int _price;
};
Computer *Computer :: instance = nullptr;

void test(){
    Computer *c1 = Computer::get_instance("iqoo",4500);
    c1->print();
    Computer *c2 = Computer::get_instance("ooqi",1234);
    c2->print();
    cout<<(c1 == c2)<<endl;
    c2->destroy();
    c1->destroy();//测试不会报错

}

int main()
{


    test ();
    return 0;
}

